package com.db.chat.server.checkname;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by Student on 25.11.2014.
 */
public class NickNameChecker {
    private Set<String> nickNames = new HashSet<String>();
    private Set<String> roomNames = new HashSet<String>();
    private static final String incorrectNameMessage = "Incorrect name. Please try again.";
    private static final String incorrectRoomNameMessage = "Incorrect room name. Please try again.";
    private static final String messageNickNameLong = "Nickname is too long. Please try again.";
    private static final String messageRoomNameLong = "Nickname is too long. Please try again.";
    private static final String duplicateNickNameMess = "This name already exists. Please try again.";
    private static final String duplicateRoomNameMess = "This name already exists. Please try again.";

    public void registerName(String nickName) throws NickNameException {
        if (nickName.contains(" "))
            throw new NickNameException(incorrectNameMessage);
        else if (nickName.length() > 150) {
            throw new NickNameException(messageNickNameLong);
        } else if (!nickNames.add(nickName)) {
                throw new NickNameException(duplicateNickNameMess);
        }
    }

    public void registerRoom(String roomName) throws RoomNameException {
        if (roomName.contains(" "))
            throw new RoomNameException(incorrectRoomNameMessage);
        else if (roomName.length() > 150) {
            throw new RoomNameException(messageRoomNameLong);
        } else if (!roomNames.add(roomName)) {
            throw new RoomNameException(duplicateRoomNameMess);
        }
    }

    public void renameNickName(String lastName, String newName) {
        nickNames.remove(lastName);
        nickNames.add(newName);
    }
}
